﻿using System;
using Codemonk.Common.Caching;

namespace Codemonk.Common.Emit
{
    public sealed class TypeAccessorFactory : ITypeAccessorFactory
    {
      
        private static ICacheCollection<Type, TypeAccessor> cache = new DictionaryCache<Type, TypeAccessor>();

        public TypeAccessor Get(string name = null)
        {
            return GetByType(name.AsType());
        }

        public TypeAccessor GetByType(Type type)
        {
            TypeAccessor accessor;
            if (cache.TryGet(type, out accessor)) return accessor;
            accessor = new TypeAccessor(type);
            cache.Set(type, accessor);
            return accessor;
        }
    }
}
